﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Grupo3.Scenes;
using Grupo3.Telas;

namespace Grupo3.Objetos
{
    public class Player : Objeto2D
    {
        
        float timer = 0;
        float interval = 80f;
        int curFrame = 0;

        int spriteWidth;
        int spriteHeight;

        Rectangle spriteRect;

        KeyboardState antigoK, atualK;
        MouseState antigoM, atualM;

        public bool isInvulnerable;
        int invulnerableCounter;

        int vida;

        public Player(Texture2D textura, Vector2 position) : base(textura, position)
        {
            vida = 3;
        }

        public void perderVida(int numero)
        {
            vida -= numero;
            isInvulnerable = true;
            invulnerableCounter = 120;

            if (vida <= 0)
            {
                SceneManager.setScene(new GameOver());
            }
        }

        private void verifyInvulnerability()
        {
            if (isInvulnerable)
                invulnerableCounter--;

            if (invulnerableCounter == 0)
            {
                isInvulnerable = false;
            }

        }

        public override void Update(GameTime gameTime)
        {
            verifyInvulnerability();

            //ANIMAÇÃO----------------------------------------
            timer += (float)gameTime.ElapsedGameTime.TotalMilliseconds;

            if (curFrame >= 6)
            {
                curFrame = 0;
            }

            if (timer >= interval)
            {
                timer = 0;
                curFrame++;
            }

            spriteRect.X = curFrame * spriteWidth;

            antigoK = atualK;
            atualK = Keyboard.GetState();

            //TECLADO--------------------------------------------
            if (atualK.IsKeyDown(Keys.Up))
                position.Y -= 10;

            if (atualK.IsKeyDown(Keys.Down))
                position.Y += 10;

            if (atualK.IsKeyDown(Keys.Left))
                position.X -= 10;
            
            if (atualK.IsKeyDown(Keys.Right))
                position.X += 10;

            
            base.Update(gameTime);
        }

        public override void Draw(SpriteBatch spriteBatch)
        {
            spriteBatch.Draw(texture, position, Color.White);
        }
    }
}
